package com.xbf.controller;

import com.xbf.dto.CartGoodsDTO;
import com.xbf.entity.Shop;
import com.xbf.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @PostMapping("/add")
    public Map<String, Object> addToCart(@RequestBody Shop shop) {
        Map<String, Object> result = new HashMap<>();
        boolean success = shopService.addToCart(shop);
        result.put("success", success);
        result.put("message", success ? "添加到购物车成功" : "添加到购物车失败");
        return result;
    }

    @GetMapping("/user/{userId}")
    public List<Shop> getCartByUserId(@PathVariable Integer userId) {
        return shopService.getCartByUserId(userId);
    }

//    @PutMapping("/update")
//    public boolean updateCartItem(@RequestBody Shop shop) {
//        return shopService.updateCartItem(shop);
//    }

    @DeleteMapping("/{id}")
    public boolean deleteCartItem(@PathVariable Integer id) {
        return shopService.deleteCartItem(id);
    }

    @GetMapping("/cart/{userId}")
    public List<CartGoodsDTO> getCartGoods(@PathVariable Integer userId) {
        return shopService.getCartGoods(userId);
    }

    @PostMapping("/updateCart")
    public Map<String, Object> updateCartItem(@RequestBody Shop shop) {
        Map<String, Object> result = new HashMap<>();
        boolean success = shopService.updateCartItem(shop);
        result.put("success", success);
        result.put("message", success ? "更新成功" : "更新失败");
        return result;
    }

}